package com.gitee.wsl.transform.validator.annotation

import com.gitee.wsl.net.url.encodeURLPath
import kotlin.collections.Map

internal fun Any?.isString() = this is String

internal fun Any?.hasSize(min: Int = 0, max: Int? = null): Boolean {
    var n = 0
    when (this) {
        is Iterable<*> -> n = this.count()
        is Map<*, *> -> n = this.size
        is String -> n = this.length
    }
    return n >= min && (max == null || n <= max)
}

internal fun String.isByteLength(min: Int = 0, max: Int? = null): Boolean {
    val len = encodeURLPath().split(Regex("%..|.")).size - 1
    return len >= min && (max == null || len <= max)
}